﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Comod_SupplierManage : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            hfuserId.Value = Request.Cookies["user"].Values["id"].ToString();
            
            initGvData();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("SupplierAdd.aspx?moduid=" + Request["moduid"] + "&action=add");
    }

    private void initGvData()
    {
        string sql = "select  *,(SELECT COUNT(0) FROM CS_Stock  WHERE SuppliId = CS_Supplier.SupplierId) AS Stock, convert(char(10),editDate,120) as eidtdat from CS_Supplier order by SupplierId desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }



    protected void btnFlag_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {

                    zs.Open("update  CS_Supplier set AuditState='已通过' where SupplierId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }

    }
    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("SupplierAdd.aspx?id=" + id + "&action=update&moduid=" + Request["moduid"]);
    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("SupplierAdd.aspx?id=" + id + "&action=flag&moduid=" + Request["moduid"]);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {

                    zs.Open("delete CS_Supplier  where SupplierId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string sql = "select  *,(SELECT COUNT(0) FROM CS_Stock  WHERE SuppliId = CS_Supplier.SupplierId) AS Stock,convert(char(10),editDate,120) as eidtdat from CS_Supplier";
        if (txtContent.Text != "")
        {
            if (txtuser.Text == "")
            {
                if (txtadres.Text == "")
                {
                    sql += " where Supplier like'%" + Common.FormatParameter(txtContent.Text) + "%'";
                }
                else
                {
                    sql += " where Supplier like'%" + Common.FormatParameter(txtContent.Text) + "%' and Address like'%" + Common.FormatParameter(txtadres.Text) + "%'";
                }

            }

        }
        if (txtContent.Text != "")
        {
            if (txtuser.Text != "")
            {
                if (txtadres.Text != "")
                {
                    sql += " where Supplier like'%" + Common.FormatParameter(txtContent.Text) + "%'and Address like'%" + Common.FormatParameter(txtadres.Text) + "%'and suppMan like'%" + Common.FormatParameter(txtuser.Text) + "%'";
                }
                else
                {
                    sql += " where  Supplier like'%" + Common.FormatParameter(txtContent.Text) + "%' and suppMan like'%" + Common.FormatParameter(txtuser.Text) + "%'";
                }
            }

        }
        if (txtContent.Text == "")
        {
            if (txtuser.Text == "")
            {
                if (txtadres.Text != "")
                {
                    sql += " where Address like'%" + Common.FormatParameter(txtadres.Text) + "%'";
                }
                else
                {
                    sql += "";
                }
            }

        }
        if (txtContent.Text == "")
        {
            if (txtuser.Text != "")
            {
                if (txtadres.Text != "")
                {
                    sql += " where  suppMan like'%" + Common.FormatParameter(txtuser.Text) + "%' and Address like'%" + Common.FormatParameter(txtadres.Text) + "%'";
                }
                else
                {
                    sql += " where suppMan like'%" + Common.FormatParameter(txtuser.Text) + "%'";
                }
            }

        }
        sql += " order by SupplierId desc";
            PageNavigator1.Total = 1;
            PageNavigator1.Query = sql;
   
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 10, GvData, btnDel, btnFlag, btnAdd);
        //删除是否可用
        /*string delDisabled = DataBinder.Eval(e.Row.DataItem, "Stock").ToString();
        if (Convert.ToInt32(delDisabled) > 0)
        {
            (e.Row.FindControl("chkDelete") as CheckBox).Enabled = false;
        }*/
    }
}
